<template>
  <basic-container>
    <avue-crud
      :option="option"
      :data="data"
      :page.sync="page"
      :search.sync="query"
      :permission="permissionList"
      :before-open="beforeOpen"
      :table-loading="loading"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="handleExport">导出</el-button>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
    </avue-crud>
    <export-comp
      ref="exportComp"
      page-api-url="mes-abacus/warehouse-report/page-query-outbound-record"
      :option="option"
      :page="page"
      :query="exportParams"
      file-name="物料出库记录"
    >
    </export-comp>
  </basic-container>
</template>

<script>
import { getList } from '@/api/warehouse/materialOutboundRecord';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';

export default {
  components: {
    exportComp,
    userTableConfig
  },
  mixins: [userConfig()],
  name: 'materialOutboundRecord',
  data() {
    return {
      form: {},
      query: {
        time: this.getDefaultTime()
      },
      exportParams: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      data: [],
      option: {
        height: 'calc(100vh - 540px)',
        columnBtn: false,
        calcHeight: 30,
        tip: false,
        refreshBtn: true, //刷新按钮是否显示
        searchBtn: true, // 显示查询的按钮
        border: true,
        index: true,
        dialogClickModal: false,
        addBtn: false,
        delBtn: false,
        editBtn: false,
        viewBtn: false,
        width: '100',
        menu: false,
        searchLabelWidth: 100,
        searchMenuPosition: 'right',
        searchMenuSpan: 24,
        searchSpan: 6,
        searchShow: true,
        indexLabel: '序号',
        column: [
          {
            label: '出库时间',
            prop: 'time',
            searchType: 'daterange',
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期',
            hide: true,
            searchSpan: 6,
            searchOrder: 1,
            searchValue: this.getDefaultTime(),
            search: true
          },
          {
            label: '出库时间',
            prop: 'outboundTime'
          },
          {
            label: '单据类型',
            prop: 'sheetType',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sheet_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '单据号',
            prop: 'sheetNo',
            search: true,
            searchOrder: 2
          },
          {
            label: '物料名称',
            prop: 'partName'
          },
          {
            label: '物料编码',
            prop: 'partNo',
            search: true,
            searchOrder: 6
          },
          {
            label: '包装条码',
            prop: 'packNo',
            search: true,
            searchOrder: 3
          },
          {
            label: '包装数量',
            prop: 'packQuantity'
          },
          {
            label: '包装状态',
            prop: 'packStatus',
            type: 'select',
            dicData: [
              {
                label: '正常',
                value: 1
              },
              {
                label: '全部锁定',
                value: 2
              },
              {
                label: '部分锁定',
                value: 3
              },
              {
                label: '报废',
                value: 4
              },
              {
                label: '已出库',
                value: 5
              },
              {
                label: '已入库',
                value: 6
              }
            ]
          },
          {
            label: '仓库名称',
            prop: 'inventoryName'
          },
          {
            label: '仓库编码',
            prop: 'inventoryCode'
          },
          {
            label: '储位名称',
            prop: 'areaName'
          },
          {
            label: '储位编码',
            prop: 'areaCode'
          },
          {
            label: '物料规格',
            prop: 'partModel'
          },
          {
            label: '生产日期',
            prop: 'productionDate'
          },
          {
            label: '供应商名称',
            prop: 'vendorName'
          },
          {
            label: '物料批次',
            prop: 'lotNo',
            search: true,
            searchOrder: 5
          },
          {
            label: '供应商批次',
            prop: 'vendorLotNo',
            search: true,
            searchOrder: 4
          },
          {
            label: 'D/C',
            prop: 'dateCode'
          },
          {
            label: '品牌',
            prop: 'materialBrand'
          },
          {
            label: '操作人',
            prop: 'operatorName'
          },
          {
            label: '回写状态',
            prop: 'writeBackStatus',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=WRITE_BACK_STATUS',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          }
        ]
      }
    };
  },
  filters: {},
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {};
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.sheetPackingId);
      });
      return ids.join(',');
    }
  },
  methods: {
    beforeOpen(done, type) {
      done();
    },
    searchReset(params, done) {
      this.query = {};
      this.page.currentPage = 1;
      this.page.pageSize = 15;
      this.onLoad(this.page, this.query);
    },
    searchChange(params, done) {
      if (params.time) {
        params.startTime = params.time[0];
        params.endTime = params.time[1];
        delete params.time;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    refreshChange() {
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if ((params.time && params.time.length) || (this.query.time && this.query.time.length)) {
        params.startTime = (params.time && params.time[0]) || this.query.time[0];
        params.endTime = (params.time && params.time[1]) || this.query.time[1];
        delete params.time;
        delete this.query.time;
      }
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.data = data.records;
          this.page.total = data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    getDefaultTime() {
      const startTime = this.$moment(new Date()).format('yyyy-MM-DD');
      const endTime = this.$moment(new Date()).format('yyyy-MM-DD');
      return [startTime, endTime];
    },
    handleExport() {
      this.$refs.exportComp.visible = true;
      this.exportParams = this.query;
      if (this.exportParams.time) {
        this.exportParams.startTime = params.time[0];
        this.exportParams.endTime = params.time[1];
        delete this.exportParams.time;
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },

  watch: {}
};
</script>

<style scoped></style>
